<?php

class PicturesController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
        $pub = Pub::find(1);
        require_once app_path().'/lib/wjs/jssdk.php';
        $jssdk = new JSSDK($pub->AppID,$pub->AppSecret);
        $signPackage = $jssdk->GetSignPackage();
        $mount_id = Input::get('mount_id');
        return View::make('pictures.create',compact('mount_id','signPackage'));

	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
        $mount_id = Input::get('mount_id');
        require app_path().'/lib/qiniu/rs.php';
        require app_path().'/lib/qiniu/io.php';
        $pub = Pub::find(1);
        if (Cache::has('token'))
        {
            //
            $value = Cache::get('token');
        }else{
            $value =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $value, 240);
        }

        $mediaid = Input::get('media_id');
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$value&media_id=$mediaid";

        $contents = @file_get_contents($url);
//
        $uploadfile = $this->random(5).time().'.jpg';
        $result = $this->upPicTo7niu($contents,'com',$uploadfile,'aebo');

        if($mount_id == ''){
           var_dump(Input::all());
            exit;
        }
        $picture = new Picture();
        $picture->path = "http://aebo.qiniudn.com/".$result['key'];
        $picture->mount_id = $mount_id;
        $picture->save();


        return Redirect::route('mounts.pictures');
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
        $mount = Mount::find($id);
        return View::make('pictures.show',compact('mount'));
//        var_dump($rates);
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
        $pub = Pub::find(1);
        require_once app_path().'/lib/wjs/jssdk.php';
        $jssdk = new JSSDK($pub->AppID,$pub->AppSecret);
        $signPackage = $jssdk->GetSignPackage();


        return View::make('pictures.edit',compact('id','signPackage'));
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//

        require app_path().'/lib/qiniu/rs.php';
        require app_path().'/lib/qiniu/io.php';
        $pub = Pub::find(1);
        if (Cache::has('token'))
        {
            //
            $value = Cache::get('token');
        }else{
            $value =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $value, 240);
        }

        $mediaid = Input::get('media_id');
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$value&media_id=$mediaid";

        $contents = @file_get_contents($url);
//
        $uploadfile = $this->random(5).time().'.jpg';
        $result = $this->upPicTo7niu($contents,'com',$uploadfile,'aebo');
        var_dump($result);

	}

    public function upload(){
        require app_path().'/lib/qiniu/rs.php';
        require app_path().'/lib/qiniu/io.php';
        $pub = Pub::find(1);
        if (Cache::has('token'))
        {
            //
            $value = Cache::get('token');
        }else{
            $value =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $value, 240);
        }

        $mediaid = Input::get('media_id');
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$value&media_id=$mediaid";

        $contents = @file_get_contents($url);
//
        $uploadfile = $this->random(5).time().'.jpg';
        $result = $this->upPicTo7niu($contents,'com',$uploadfile,'aebo');

        $picture = Picture::find(Input::get('id'));
        $picture->path = "http://aebo.qiniudn.com/".$result['key'];
        $picture->save();
        return Redirect::route('mounts.pictures');

    }

    function random($length)
    {
        $hash = 'CR-';
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
        for($i = 0; $i < $length; $i++)
        {
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }
    /**
     *上传图片至七牛云
     *$path 图片地址
     *$dirname 在七牛云上的文类名称
     *$filename 文件保存名称
     *$bucket 你的七牛云域名称
     */
    function upPicTo7niu($contents,$dirname,$filename,$bucket){

        $storename = $dirname."/".$filename;
        $bu = $bucket.":".$storename;
        $accessKey = 'FWdy0abUDmanFns5ZArKEHjRl2e5hXeZVsLKSU7M';
        $secretKey = 'zuHKtuuL888TuuavvzbyWnGnytjW33zI1mrAql12';

        Qiniu_SetKeys($accessKey, $secretKey);
        $putPolicy = new \Qiniu_RS_PutPolicy($bu);
        $upToken = $putPolicy->Token(null);
        $putExtra = new \Qiniu_PutExtra();
        $putExtra->Crc32 = 1;
        list($ret, $err) = Qiniu_Put($upToken, $storename, $contents, $putExtra);
        return $ret;
    }

    function downloadWeixinFile($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
        curl_close($ch);
        $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
        return $imageAll;
    }

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//


	}



}
